package com.osi.project.common.intercaptor;

import com.osi.start.common.business.dto.BaseVO;
import com.osi.start.common.business.dto.RespVO;
import com.osi.start.common.business.enums.ReturnCodeEnum;
import com.osi.start.common.business.service.ILocaleService;
import com.osi.start.common.util.SpringContextHolder;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotFoundException implements ErrorController {

	@Override
	public String getErrorPath() {
		return "/error";
	}

	@RequestMapping(value = { "/error" })
	@ResponseBody
	public Object error(HttpServletRequest request) {
		RespVO<BaseVO> yxRespVO = new RespVO<>(ReturnCodeEnum.MESSAGE_REQUEST_URL_ERROR);
		// 国际化处理
		String msg = convertMessage(yxRespVO.getMsg());
		yxRespVO.setMsg(msg);
		return yxRespVO;
	}

	private String convertMessage(String message) {
		ILocaleService localeService = SpringContextHolder.getBean("localeService");
		if (localeService == null) {
			return message;
		}
		return localeService.getMessage(message);
	}
}
